﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2024/7/26 11:30:36------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class products_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.products().Exists(this.id))
                {
                    JscriptMsg("产品管理不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("products", DTEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void TreeBind()
        {
            BLL.products_category bll = new BLL.products_category();
            DataTable dt = bll.GetList(0);

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("请选择类别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.products bll = new BLL.products();
            Model.products model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            ddlCategoryId.SelectedValue = model.category_id.ToString();
            txtCallIndex.Text = model.call_index;
            txtLinkUrl.Text = model.link_url;
            txtImgUrl.Text = model.img_url;
            txtSeoTitle.Text = model.seo_title;
            txtSeoKeywords.Text = model.seo_keywords;
            txtSeoDescription.Text = model.seo_description;

            if (model.is_top == 1)
            {
                cblItem.Items[0].Selected = true;
            }
            if (model.is_red == 1)
            {
                cblItem.Items[1].Selected = true;
            }
            if (model.is_hot == 1)
            {
                cblItem.Items[2].Selected = true;
            }

            txtSub_title.Text = model.sub_title;
            cbStatus.Checked = model.status == 0;
            txtSortId.Text = model.sort_id.ToString();
            txtAccuracy_level.Text = model.accuracy_level;
            txtFluid_temperature.Text = model.fluid_temperature;
            txtFlow_field_sensitivity_level.Text = model.flow_field_sensitivity_level;
            txtProtection_level.Text = model.protection_level;
            txtClimate_and_mechanical_environmental_safety.Text = model.climate_and_mechanical_environmental_safety;
            txtElectromagnetic_compatibility_level.Text = model.electromagnetic_compatibility_level;
            txtPower_waste.Text = model.power_waste;
            txtOutput_signal.Text = model.output_signal;
            txtCommunication_interface.Text = model.communication_interface;
            txtPower_supply.Text = model.power_supply;
            txtWork_environment.Text = model.work_environment;
            txtPressure_loss.Text = model.pressure_loss;
            txtData_storage.Text = model.data_storage;
            txtExecution_standards.Text = model.execution_standards;
            txtContent.Value = model.contents;
            txtAbstracts.Text = model.abstracts;

            txtAddTime.Text = Utils.ObjectToDateTime(model.add_time, DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss");

            rptAlbumList.DataSource = model.products_albums;
            rptAlbumList.DataBind();
            //绑定内容附件
            rptAttachList.DataSource = model.products_attaches;
            rptAttachList.DataBind();

            //绑定类别属性
            rptAttributeList.DataSource = new BLL.products_attribute_field().GetProductsAttributeList(model.id);
            rptAttributeList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.products model = new Model.products();
            BLL.products bll = new BLL.products();

            model.title = txtTitle.Text;
            model.category_id = int.Parse(ddlCategoryId.SelectedValue);
            model.call_index = txtCallIndex.Text;
            model.link_url = txtLinkUrl.Text;
            model.img_url = txtImgUrl.Text;
            model.seo_title = txtSeoTitle.Text;
            model.seo_keywords = txtSeoKeywords.Text;
            model.seo_description = txtSeoDescription.Text;

            model.is_top = 0;
            model.is_red = 0;
            model.is_hot = 0;

            if (cblItem.Items[0].Selected == true)
            {
                model.is_top = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.is_red = 1;
            }
            if (cblItem.Items[2].Selected == true)
            {
                model.is_hot = 1;
            }

            model.sub_title = txtSub_title.Text;
            model.status = cbStatus.Checked ? 0 : 1;
            model.sort_id = int.Parse(txtSortId.Text);
            model.accuracy_level = txtAccuracy_level.Text;
            model.fluid_temperature = txtFluid_temperature.Text;
            model.flow_field_sensitivity_level = txtFlow_field_sensitivity_level.Text;
            model.protection_level = txtProtection_level.Text;
            model.climate_and_mechanical_environmental_safety = txtClimate_and_mechanical_environmental_safety.Text;
            model.electromagnetic_compatibility_level = txtElectromagnetic_compatibility_level.Text;
            model.power_waste = txtPower_waste.Text;
            model.output_signal = txtOutput_signal.Text;
            model.communication_interface = txtCommunication_interface.Text;
            model.power_supply = txtPower_supply.Text;
            model.work_environment = txtWork_environment.Text;
            model.pressure_loss = txtPressure_loss.Text;
            model.data_storage = txtData_storage.Text;
            model.execution_standards = txtExecution_standards.Text;
            model.contents = txtContent.Value;
            model.abstracts = txtAbstracts.Text;
            model.add_time = DateTime.Now;

            #region 保存相册====================
            //检查是否有自定义图片
            if (txtImgUrl.Text.Trim() == "")
            {
                model.img_url = hidFocusPhoto.Value;
            }
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null && albumArr.Length > 0)
            {
                List<Model.products_albums> ls = new List<Model.products_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    if (imgArr.Length == 3)
                    {
                        if (!string.IsNullOrEmpty(remarkArr[i]))
                        {
                            ls.Add(new Model.products_albums { original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                        }
                        else
                        {
                            ls.Add(new Model.products_albums { original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                }
                model.products_albums = ls;
            }
            #endregion

            #region 保存附件====================
            //保存附件
            string[] attachFileNameArr = Request.Form.GetValues("hid_attach_filename");
            string[] attachFilePathArr = Request.Form.GetValues("hid_attach_filepath");
            string[] attachFileSizeArr = Request.Form.GetValues("hid_attach_filesize");

            if (attachFileNameArr != null && attachFilePathArr != null && attachFileSizeArr != null
                && attachFileNameArr.Length > 0 && attachFilePathArr.Length > 0 && attachFileSizeArr.Length > 0)
            {
                List<Model.products_attach> ls = new List<Model.products_attach>();
                for (int i = 0; i < attachFileNameArr.Length; i++)
                {
                    int fileSize = Utils.StrToInt(attachFileSizeArr[i], 0);
                    string fileExt = FileHelper.GetFileExt(attachFilePathArr[i]);

                    ls.Add(new Model.products_attach { file_name = attachFileNameArr[i], file_path = attachFilePathArr[i], file_size = fileSize, file_ext = fileExt });
                }
                model.products_attaches = ls;
            }
            #endregion

            #region 保存规格====================
                        
            string[] hid_field_id = Request.Form.GetValues("hid_field_id");
            string[] hid_display = Request.Form.GetValues("hid_display");
            string[] sort_id = Request.Form.GetValues("sort_id");
            if (hid_field_id != null && hid_display != null && txtSortId != null)
            {
                if (hid_field_id.Length > 0 && hid_display.Length > 0 && sort_id.Length > 0)
                {
                    List<Model.products_field> pfls = new List<Model.products_field>();

                    for (int i = 0; i < hid_field_id.Length; i++)
                    {
                        int _display = Utils.ObjToInt(hid_display[i]);
                        int _sort_id = Utils.ObjToInt(sort_id[i], 99);
                        int _field_id = Utils.ObjToInt(hid_field_id[i]);

                        pfls.Add(new Model.products_field { display = _display, field_id = _field_id, sort_id = _sort_id });
                    }
                    model.products_fields = pfls;
                }
            }

            #endregion

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加产品管理成功！"); //产品管理日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.products bll = new BLL.products();
            Model.products model = bll.GetModel(_id);

            model.title = txtTitle.Text;
            model.category_id = int.Parse(ddlCategoryId.SelectedValue);
            model.call_index = txtCallIndex.Text;
            model.link_url = txtLinkUrl.Text;
            model.img_url = txtImgUrl.Text;
            model.seo_title = txtSeoTitle.Text;
            model.seo_keywords = txtSeoKeywords.Text;
            model.seo_description = txtSeoDescription.Text;

            model.is_top = 0;
            model.is_red = 0;
            model.is_hot = 0;

            if (cblItem.Items[0].Selected == true)
            {
                model.is_top = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.is_red = 1;
            }
            if (cblItem.Items[2].Selected == true)
            {
                model.is_hot = 1;
            }

            model.sub_title = txtSub_title.Text;
            model.status = cbStatus.Checked ? 0 : 1;
            model.sort_id = int.Parse(txtSortId.Text);
            model.accuracy_level = txtAccuracy_level.Text;
            model.fluid_temperature = txtFluid_temperature.Text;
            model.flow_field_sensitivity_level = txtFlow_field_sensitivity_level.Text;
            model.protection_level = txtProtection_level.Text;
            model.climate_and_mechanical_environmental_safety = txtClimate_and_mechanical_environmental_safety.Text;
            model.electromagnetic_compatibility_level = txtElectromagnetic_compatibility_level.Text;
            model.power_waste = txtPower_waste.Text;
            model.output_signal = txtOutput_signal.Text;
            model.communication_interface = txtCommunication_interface.Text;
            model.power_supply = txtPower_supply.Text;
            model.work_environment = txtWork_environment.Text;
            model.pressure_loss = txtPressure_loss.Text;
            model.data_storage = txtData_storage.Text;
            model.execution_standards = txtExecution_standards.Text;
            model.contents = txtContent.Value;
            model.abstracts = txtAbstracts.Text;

            #region 保存相册====================
            //检查是否有自定义图片
            if (txtImgUrl.Text.Trim() == "")
            {
                model.img_url = hidFocusPhoto.Value;
            }
            if (model.products_albums != null)
            {
                model.products_albums.Clear();
            }
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null)
            {
                List<Model.products_albums> ls = new List<Model.products_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    int img_id = Utils.StrToInt(imgArr[0], 0);
                    if (imgArr.Length == 3)
                    {
                        if (!string.IsNullOrEmpty(remarkArr[i]))
                        {
                            ls.Add(new Model.products_albums { id = img_id, products_id = _id, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] });
                        }
                        else
                        {
                            ls.Add(new Model.products_albums { id = img_id, products_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                }
                model.products_albums = ls;
            }
            #endregion

            #region 保存附件====================
            if (model.products_attaches != null)
            {
                model.products_attaches.Clear();
            }
            string[] attachIdArr = Request.Form.GetValues("hid_attach_id");
            string[] attachFileNameArr = Request.Form.GetValues("hid_attach_filename");
            string[] attachFilePathArr = Request.Form.GetValues("hid_attach_filepath");
            string[] attachFileSizeArr = Request.Form.GetValues("hid_attach_filesize");

            if (attachIdArr != null && attachFileNameArr != null && attachFilePathArr != null && attachFileSizeArr != null
                && attachIdArr.Length > 0 && attachFileNameArr.Length > 0 && attachFilePathArr.Length > 0 && attachFileSizeArr.Length > 0)
            {
                List<Model.products_attach> ls = new List<Model.products_attach>();
                for (int i = 0; i < attachFileNameArr.Length; i++)
                {
                    int attachId = Utils.StrToInt(attachIdArr[i], 0);
                    int fileSize = Utils.StrToInt(attachFileSizeArr[i], 0);
                    string fileExt = FileHelper.GetFileExt(attachFilePathArr[i]);
                    ls.Add(new Model.products_attach { id = attachId, products_id = _id, file_name = attachFileNameArr[i], file_path = attachFilePathArr[i], file_size = fileSize, file_ext = fileExt, });
                }
                model.products_attaches = ls;
            }
            #endregion

            #region 保存规格====================
            if (model.products_fields != null)
            {
                model.products_fields.Clear();
            }

            string[] hid_id = Request.Form.GetValues("hid_id");
            string[] hid_field_id = Request.Form.GetValues("hid_field_id");
            string[] hid_display = Request.Form.GetValues("hid_display");
            string[] sort_id = Request.Form.GetValues("sort_id");
            if (hid_id != null && hid_field_id != null && hid_display != null && txtSortId != null)
            {
                if (hid_id.Length > 0 && hid_field_id.Length > 0 && hid_display.Length > 0 && sort_id.Length > 0)
                {
                    List<Model.products_field> pfls = new List<Model.products_field>();
                    for (int i = 0; i < hid_field_id.Length; i++)
                    {
                        int _display = Utils.ObjToInt(hid_display[i]);
                        int _sort_id = Utils.ObjToInt(sort_id[i], 99);
                        int _field_id = Utils.ObjToInt(hid_field_id[i]);
                        int _itemId = Utils.ObjToInt(hid_id[i]);

                        pfls.Add(new Model.products_field { id = _itemId, display = _display, field_id = _field_id, sort_id = _sort_id, products_id = model.id });
                    }
                    model.products_fields = pfls;
                }
            }
                         

            #endregion

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改产品管理成功！"); //产品管理日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("products", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改产品管理成功！", "products_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("products", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加产品管理成功！", "products_list.aspx");
            }
        }

    }
}

